package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import net.realsee.common.utils.StringUtils;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import net.realsee.domain.wcs.data.bo.WcsDefectCodeBo;
import net.realsee.domain.wcs.data.vo.WcsDefectCodeVo;
import net.realsee.domain.wcs.data.WcsDefectCode;
import net.realsee.mapper.wcs.data.WcsDefectCodeMapper;
import net.realsee.service.wcs.data.IWcsDefectCodeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 系统内不良代码定义Service业务层处理
 *
 * @author realsee
 * @date 2023-08-24
 */
@RequiredArgsConstructor
@Service
public class WcsDefectCodeServiceImpl extends ServiceImpl<WcsDefectCodeMapper, WcsDefectCode> implements IWcsDefectCodeService {

    private final WcsDefectCodeMapper baseMapper;

    /**
     * 查询系统内不良代码定义
     */
    @Override
    public WcsDefectCodeVo selectVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询系统内不良代码定义列表
     */
    @Override
    public TableDataInfo<WcsDefectCodeVo> selectVoPage(WcsDefectCodeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsDefectCode> lqw = buildVoQueryWrapper(bo);
        Page<WcsDefectCodeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询系统内不良代码定义列表
     */
    @Override
    public List<WcsDefectCodeVo> selectVoList(WcsDefectCodeBo bo) {
        LambdaQueryWrapper<WcsDefectCode> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsDefectCode> buildVoQueryWrapper(WcsDefectCodeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsDefectCode> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDefectCode()), WcsDefectCode::getDefectCode, bo.getDefectCode());
        lqw.eq(StringUtils.isNotBlank(bo.getSeverityCode()), WcsDefectCode::getSeverityCode, bo.getSeverityCode());
        return lqw;
    }

    /**
     * 新增系统内不良代码定义
     */
    @Override
    public Boolean insertByBo(WcsDefectCodeBo bo) {
        WcsDefectCode add = BeanUtil.toBean(bo, WcsDefectCode.class);
        return this.save(add);
    }

    /**
     * 修改系统内不良代码定义
     */
    @Override
    public Boolean updateByBo(WcsDefectCodeBo bo) {
        WcsDefectCode update = BeanUtil.toBean(bo, WcsDefectCode.class);
        return this.updateById(update);
    }

    /**
     * 批量删除系统内不良代码定义
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
